
	$(function() {
		/** 검색 */
		 $("#handle_search").click( function()	{
			 if($("#tvAppToday input:text[name=startDate]").val() > $("#tvAppToday input:text[name=endDate]").val() ){
					alert("날짜 검색 범위를 올바르게 지정해 주세요.");
					return;
				}
			 
			 var f = $("#tvAppToday").attr({action : "tvAppToday.omp"});
			 $("#flag").val("Y");
			 f.submit();
		 });
		/** 초기화 */
		 $("#handle_reset").click( function()	{
			 $("#tvAppToday").attr({action : "tvAppToday.omp"});
			 $(":hidden").val("");
			 $(":radio[name=exposure]:first").attr("checked","checked");
			 $(":radio[name=period]:first").attr("checked","checked");
			 $(":radio[name=genre]:first").attr("checked","checked");
			 $(":radio[name=broadCaster]:first").attr("checked","checked");
			 $(":radio[name=broadCaster]:first").attr("checked","checked");
			 $(":radio[name=period]:first").click();
		 });
		/** Check Box 전체 선택 / 해제 */
		 $("#handle_check").click( function()	{
			 if(this.checked)
				 $(":checkbox[name=content]").each( function(){
					 this.checked = true;
				 });
			 else
				 $(":checkbox[name=content]").each( function(){
					 this.checked = false;
				 });
		 });
		/** 등록하기 - 팝업 */
		 $("#handle_register").click( function() {
			 window.open('popupTvAppToday.omp','register','width=800,height=800,toobar=no,scrollbars=yes,menubar=no,status=no ,directories=no');
		 });
		/** 엑셀다운 - Excel */
		 $("#handle_btn_excel").click( function()	{
			 var f = $("#tvAppToday").attr({action : "tvAppTodayByExcelDownload.omp"});
			 $(":hidden").val("");
			 $("#flag").val("Y");
			 $(":radio[name=exposure]:first").attr("checked","checked");
			 $(":radio[name=period]:first").attr("checked","checked");
			 $(":radio[name=genre]:first").attr("checked","checked");
			 $(":radio[name=broadCaster]:first").attr("checked","checked");
			 $(":radio[name=broadCaster]:first").attr("checked","checked");
			 $("#startDate").val("");
			 $("#endDate").val("");
			 f.submit();
		 });
		/** 검색 - Combo Box */
		 $("#handle_combo").change( function(){
			 var f = $("#tvAppToday").attr({action : "tvAppToday.omp"});
			 $("#flag").val("Y");
			 $("#pageNum").val("1");
			 $("#pageSize").val($("#handle_combo").val());
			 f.submit();
		 });
		
		/** 페이징 */
		 $("a[name=paging]").each(function(){
			 this.onclick = function(){
				 var f = $("#tvAppToday").attr({action : "tvAppToday.omp"});
				 $("#flag").val("Y");
				 $("#pageNum").val(this.getAttribute("value"));
				 f.submit();
			 };
		 });
		/** 노출 / 숨김 / 삭제 */
		 $("#handle_exposure, #handle_hide, #handle_delete").click( function(){
			 var status = this.getAttribute("mode");
			 if($(":checkbox[name=content]:checked").size()==0){
				 alert("선택된 항목이 없습니다.");
			 }else{
				 var f = $("#tvAppToday").attr({action : "tvAppTodayExposure.omp"});
				 var contentList = "";
				 $(":checkbox[name=content]:checked").each( function(){
					 contentList = contentList + $(this).attr("value") + "|";
				 });
				 $("#content").val(contentList);
				 $("#status").val(status);
				 f.submit();
			 }
		 });
		 
		/** 순서빈값 / 순서적용 */
		 $("#handle_order_remove, #handle_order_apply").click( function(){
			 var status = this.getAttribute("mode");
			 var size = parseInt($(":checkbox[name=content]").size());
			 var f = $("#tvAppToday").attr({action : "tvAppTodayOrder.omp"});
			 var orderList = "";
			 var orderKeyList = "";
			 for(var i=0;i<size;i++){
				 var order = $(":text[name=order]")[i].getAttribute("value");
				 var key = $(":text[name=order]")[i].getAttribute("key");
				 if((status=="U"&&!(!isNaN(order)||!order))||parseInt(order)<1){
					 alert("입력이 올바르지 않습니다.");
					 return ;
				 }
				 if(status=="U"&&(order == null ||order=="" )){
					 continue;
				 }
				 orderList = orderList + order + "|";
				 orderKeyList = orderKeyList + key + "|";
			 }
			
			 if(orderList == ""){
				 alert("순서를 입력해주세요.");
			 } else {
				 $("#order").val(orderList);
				 $("#order_key").val(orderKeyList);
				 $("#status").val(status);
				 f.submit();
			 }
		 });		 
		 
		
		/** 기간 설정 : 날짜 제어 - 주단위 */
		$(":radio[name=period]").click( function(){
			if(this.checked){
				setDatePeriod(this.value);
			}
		});
		
		if($("#flag").val() ==""){
			$(":radio[name=period]")[0].click();
		}
	});
	
	/**
	 * 기간 설정 Date 입력 처리 Function
	 * @param period
	 * @return
	 */
	function setDatePeriod(weeks){
		var period = (parseInt(weeks)*7)-1;
		var current_date = new Date();
		var before_date = new Date(Date.parse(current_date) - parseInt(period)*1000*60*60*24);
		
		var before_yyyy = before_date.getFullYear();
		var before_mm = before_date.getMonth()+1;
		var before_dd = before_date.getDate();
		before_mm = before_mm<10 ? "0"+before_mm : before_mm;
		before_dd = before_dd<10 ? "0"+before_dd : before_dd;
		
		var startDate = before_yyyy+"-"+before_mm+"-"+before_dd;
		
		var current_yyyy = current_date.getFullYear();
		var current_mm = current_date.getMonth()+1;
		var current_dd = current_date.getDate();
		current_mm = current_mm<10 ? "0"+current_mm : current_mm;
		current_dd = current_dd<10 ? "0"+current_dd : current_dd;
		
		var endDate = current_yyyy+"-"+current_mm+"-"+current_dd;
		
		/**
		 * Set startDate
		 * Set endDate
		 */
		$("#startDate").val(startDate);
		$("#endDate").val(endDate);
	}
	
	
